package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.data.tenant.TenantContextHolder;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.entity.BasicVideoModelEntity;
import com.iwomy.secureplat.platform.entity.BasicVideoProviderTypeEntity;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.service.BasicVideoModelService;
import com.iwomy.secureplat.platform.service.BasicVideoProviderTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 视频类设备服务商类型表
 *
 * @author pig
 * @date 2024-02-18 15:21:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicVideoProviderType" )
@Tag(description = "basicVideoProviderType" , name = "视频类设备服务商类型表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicVideoProviderTypeController {

    private final BasicVideoProviderTypeService basicVideoProviderTypeService;

    private final BasicVideoModelService basicVideoModelService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param basicVideoProviderType 视频类设备服务商类型表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_basicVideoProviderType_view')" )
    public R getBasicVideoProviderTypePage(@ParameterObject Page page, @ParameterObject BasicVideoProviderTypeEntity basicVideoProviderType) {
        return R.ok(basicVideoProviderTypeService.getVideoProviderTypePage(page,basicVideoProviderType));
    }


	/**
	 * 分页查询
	 * @param basicVideoProviderTypeEntity 视频类设备服务商类型表
	 * @return
	 */
	@Operation(summary = "分页查询" , description = "分页查询" )
	@GetMapping("/list" )
	public R getBasicDeviceTypeList( @ParameterObject BasicVideoProviderTypeEntity basicVideoProviderTypeEntity) {
		return R.ok(basicVideoProviderTypeService.getVideoProviderTypeList(basicVideoProviderTypeEntity));
	}


    /**
     * 通过id查询视频类设备服务商类型表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_basicVideoProviderType_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(basicVideoProviderTypeService.getById(id));
    }

    /**
     * 新增视频类设备服务商类型表
     * @param basicVideoProviderType 视频类设备服务商类型表
     * @return R
     */
    @Operation(summary = "新增视频类设备服务商类型表" , description = "新增视频类设备服务商类型表" )
    @SysLog("新增视频类设备服务商类型表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_basicVideoProviderType_add')" )
    public R save(@RequestBody BasicVideoProviderTypeEntity basicVideoProviderType) {
		BasicVideoProviderTypeEntity wrapperEntity = new BasicVideoProviderTypeEntity();
		wrapperEntity.setProviderName(basicVideoProviderType.getProviderName());
		wrapperEntity.setAppKey(basicVideoProviderType.getAppKey());
		wrapperEntity.setAppSecret(basicVideoProviderType.getAppSecret());
		LambdaQueryWrapper<BasicVideoProviderTypeEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicVideoProviderTypeEntity one = basicVideoProviderTypeService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one)) {
			throw new PlatformException("相同的服务商不能存在相同的appkey及appsecret");
		}
		basicVideoProviderType.setDeptId(SecurityUtils.getUser().getDeptId());
		basicVideoProviderType.setState(10);
		basicVideoProviderType.setTenantId(TenantContextHolder.getTenantId());
        return R.ok(basicVideoProviderTypeService.save(basicVideoProviderType));
    }

    /**
     * 修改视频类设备服务商类型表
     * @param basicVideoProviderType 视频类设备服务商类型表
     * @return R
     */
    @Operation(summary = "修改视频类设备服务商类型表" , description = "修改视频类设备服务商类型表" )
    @SysLog("修改视频类设备服务商类型表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_basicVideoProviderType_edit')" )
    public R updateById(@RequestBody BasicVideoProviderTypeEntity basicVideoProviderType) {
		long count = basicVideoModelService
				.count(Wrappers.lambdaQuery(new BasicVideoModelEntity()).eq(BasicVideoModelEntity::getProviderId, basicVideoProviderType.getId()));

		if (count > 0)
			return R.failed("该设备类型下有关联数据，不能编辑");
        return R.ok(basicVideoProviderTypeService.updateById(basicVideoProviderType));
    }

    /**
     * 通过id删除视频类设备服务商类型表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除视频类设备服务商类型表" , description = "通过id删除视频类设备服务商类型表" )
    @SysLog("通过id删除视频类设备服务商类型表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_basicVideoProviderType_del')" )
    public R removeById(@RequestBody Long[] ids) {
		long count = basicVideoModelService.count(Wrappers.<BasicVideoModelEntity>lambdaQuery().in(BasicVideoModelEntity::getProviderId, ids));
		if (count > 0)
			return R.failed("该设备类型下有关联数据，不能删除");
        return R.ok(basicVideoProviderTypeService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicVideoProviderType 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_basicVideoProviderType_export')" )
    public List<BasicVideoProviderTypeEntity> export(BasicVideoProviderTypeEntity basicVideoProviderType,Long[] ids) {
        return basicVideoProviderTypeService.list(Wrappers.lambdaQuery(basicVideoProviderType).in(ArrayUtil.isNotEmpty(ids), BasicVideoProviderTypeEntity::getId, ids));
    }

	/**
	 * 启用禁用
	 *
	 * @param basicVideoProviderTypeEntity
	 * @return R
	 */
	@Operation(summary = "启用禁用视频类设备服务商类型", description = "启用禁用视频类设备服务商类型")
	@SysLog(expression = "'启用禁用视频类设备服务商类型'.concat(#basicVideoProviderTypeEntity.remarks)")
	@PostMapping("/enableOrDisable")
	public R enableOrDisable(@RequestBody BasicVideoProviderTypeEntity basicVideoProviderTypeEntity) {
		return R.ok(basicVideoProviderTypeService.updateById(basicVideoProviderTypeEntity));
	}
}